package com.work.util;

import org.apache.commons.lang.time.DateUtils;

import java.lang.management.ManagementFactory;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtil extends DateUtils {
    /**
     * 获取服务启动时间
     */
    public static Date getServerStartDate(){
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 获取服当周第一天  无参
     */
    public static String getFirstDayOfWeek(){
        LocalDate date = LocalDate.now();
        LocalDate firstDay = date.with(DayOfWeek.MONDAY);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String firstDate = firstDay.format(formatter);
        return firstDate;
    }

    /**
     * 获取服当周第一天  有参
     */
    public static String getFirstDayOfWeek(String rateTime){
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse(rateTime,fmt);
        LocalDate firstDay = date.with(DayOfWeek.MONDAY);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String firstDate = firstDay.format(formatter);
        return firstDate;
    }


    /**
     * 获取服当周最后一天  无参
     */
    public static String getLastDayOfWeek(){
        LocalDate date = LocalDate.now();
        LocalDate firstDay = date.with(DayOfWeek.SUNDAY);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String firstDate = firstDay.format(formatter);
        return firstDate;
    }

    /**
     * 获取服当周最后一天  有参
     */
    public static String getLastDayOfWeek(String rateTime){
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse(rateTime,fmt);
        LocalDate firstDay = date.with(DayOfWeek.MONDAY);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String firstDate = firstDay.format(formatter);
        return firstDate;
    }
}
